﻿@{
    ViewBag.Title = "供应商列表";
}

<div class="row">
    <div class="col-xs-12">
        <p>
            <div class="row">
                <div class="col-sm-6">
                    <div class="input-group-btn">
                        状态:
                        <select id="query-status">
                            <option value="1" selected="selected">正常</option>
                            <option value="4">等待审核</option>
                            <option value="2">已禁用</option>
                            <option value="3">已删除</option>
                        </select>
                        <a class="btn btn-sm btn-primary" id="btnNew">
                            <i class="icon-plus bigger-110"></i>创建
                        </a>
                        <button class="btn btn-sm btn-danger" id="btnDelete">
                            <i class="icon-remove bigger-110"></i>删除
                        </button>
                        <button class="btn btn-sm btn-primary" id="btnApprove" style="display:none">
                            <i class="icon-plus bigger-110"></i>通过
                        </button>
                        <button class="btn btn-sm btn-danger" id="btnReject" style="display:none">
                            <i class="icon-remove bigger-110"></i>不通过
                        </button>
                    </div>
                </div>
                <div class="col-sm-6 right">
                </div>
            </div>
        </p>
        <div class="table-responsive">
            <table id="businessTable" class="table table-striped table-bordered table-hover">
                <thead>
                    <tr>
                        <th><input type="checkbox" id="selectAll" /></th>
                        <th>编码</th>
                        <th>名称</th>
                        <th>联系人</th>
                        <th>联系人电话</th>
                        <th>状态</th>
                        <th>操作</th>
                    </tr>
                </thead>
            </table>
        </div>
    </div>
</div>
<!-- 模态框（Modal） -->
<div class="modal fade" id="newModal" tabindex="-1" role="dialog"
     aria-labelledby="newModalLabel" aria-hidden="true">
    <div class="modal-dialog" style="width:60%;">
        <div class="modal-content">
            <div class="modal-header">
                @*<button type="button" class="close"
                            data-dismiss="modal" aria-hidden="true">
                        &times;
                    </button>*@
                <h4 class="modal-title" id="newModalLabel">
                    创建供应商
                </h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal" role="form" id="formNew">
                    <div class="form-group">
                        <label class="col-sm-1 control-label no-padding-right" for="newNumber"> 编号 </label>
                        <div class="col-sm-3">
                            <input type="text" id="newNumber" placeholder="输入编号..." class="col-xs-12 col-sm-12" />
                        </div>
                        <label class="col-sm-1 control-label no-padding-right" for="newName"> 名称 </label>
                        <div class="col-sm-3">
                            <input type="text" id="newName" placeholder="输入名称..." class="col-xs-12 col-sm-12" />
                        </div>
                    </div>
                    <div class="space-4"></div>
                    <div class="form-group">
                        <label class="col-sm-1 control-label no-padding-right" for="newEMail"> 邮箱 </label>
                        <div class="col-sm-3">
                            <input type="text" id="newEMail" class="col-xs-12 col-sm-12" />
                        </div>
                        <label class="col-sm-1 control-label no-padding-right" for="newFax"> 传真 </label>
                        <div class="col-sm-3">
                            <input type="text" id="newFax" placeholder="输入传真..." class="col-xs-12 col-sm-12" />
                        </div>
                    </div>
                    <div class="space-4"></div>
                    <div class="form-group">
                        <label class="col-sm-1 control-label no-padding-right" for="newTaxNumber"> 税号 </label>
                        <div class="col-sm-3">
                            <input type="text" id="newTaxNumber" class="col-xs-12 col-sm-12" />
                        </div>
                        <label class="col-sm-1 control-label no-padding-right" for="newBankAccount"> 银行账号 </label>
                        <div class="col-sm-3">
                            <input type="text" id="newBankAccount" class="col-xs-12 col-sm-12" />
                        </div>
                    </div>
                    <div class="space-4"></div>
                    <div class="form-group">
                        <label class="col-sm-1 control-label no-padding-right" for="newContactName"> 联系人 </label>
                        <div class="col-sm-3">
                            <input type="text" id="newContactName" placeholder="输入联系人..." class="col-xs-12 col-sm-12" />
                        </div>
                        <label class="col-sm-1 control-label no-padding-right" for="newContactTelephone"> 联系电话 </label>
                        <div class="col-sm-3">
                            <input type="text" id="newContactTelephone" placeholder="输入联系电话..." class="col-xs-12 col-sm-12" />
                        </div>
                    </div>
                    <div class="space-4"></div>
                    <div class="form-group">
                        <label class="col-sm-1 control-label no-padding-right" for="newAddress"> 地址 </label>
                        <div class="col-sm-11">
                            <input type="text" id="newAddress" placeholder="输入地址..." class="col-xs-12 col-sm-12" />
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default"
                        data-dismiss="modal">
                    关闭
                </button>
                <button type="button" class="btn btn-primary" id="btnSubmitNew">
                    提交更改
                </button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal -->
</div>
@section inlineScript
{
    <script type="text/javascript">
        var oTable = null;
        $(document).ready(function () {
            $('#com-search-input').bind('keypress', function (event) {
                if (event.keyCode == "13") {
                    queryAdvertisementList();
                }
            });
            $('#com-search-icon').on('click', function () {
                queryAdvertisementList();
            });
            $('#query-status').on('change', function () {
                if ($('#query-status').val() == 4) {
                    $('#btnApprove').show();
                    $('#btnReject').show();
                    $('#btnDelete').hide();
                } else {
                    $('#btnApprove').hide();
                    $('#btnReject').hide();
                    $('#btnDelete').show();
                }
                queryAdvertisementList();
            });
            $('#selectAll').on('change', function () {
                $('input[name=selectItem]').each(function (index, element) {
                    if (element.checked) {
                        $(element).prop('checked', false);
                    } else {
                        $(element).prop('checked', true);
                    }
                });
            });
            $('#btnDelete').on('click', function () {
                var idArray = [];
                $('input[name=selectItem]').each(function (index, element) {
                    if (element.checked) {
                        idArray.push($(element).attr('data-id'));
                    }
                });
                onDeleteMany(idArray);
            });
            $('#btnApprove').on('click', function () {
                var idArray = [];
                $('input[name=selectItem]').each(function (index, element) {
                    if (element.checked) {
                        idArray.push($(element).attr('data-id'));
                    }
                });
                onApproveMany(idArray);
            });
            $('#btnReject').on('click', function () {
                var idArray = [];
                $('input[name=selectItem]').each(function (index, element) {
                    if (element.checked) {
                        idArray.push($(element).attr('data-id'));
                    }
                });
                onRejectMany(idArray);
            });
            $('#btnNew').on('click', function () {
                $('#newModal').modal({});
            });
            $('#btnSubmitNew').on('click', function () {
                if (window.confirm('是否要创建供应商?') == false)
                    return;
                var supplier = {};
                supplier.Code = $('#newNumber').val();
                supplier.Name = $('#newName').val();
                supplier.EMail = $('#newEMail').val();
                supplier.Fax = $('#newFax').val();
                supplier.ContactName = $('#newContactName').val();
                supplier.ContactTelephone = $('#newContactTelephone').val();
                supplier.TaxNumber = $('#newTaxNumber').val();
                supplier.BandAccount = $('#newBandAccount').val();
                supplier.Address = $('#newAddress').val();

                $.ajax({
                    type: "POST",
                    url: "@Url.Content("~/api/asms/Create/Supplier")",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    data: JSON.stringify(supplier),
                    success: function (data) {
                        if (data.isSuccess) {
                            alert('创建成功');
                            $('#newModal').modal('hide');
                            queryAdvertisementList();
                        } else {
                            alert(data.message);
                        }
                    },
                    error: function (data) {
                        alert('error');
                    }
                });
            });

            queryAdvertisementList();
        });

        function queryAdvertisementList() {
            if (oTable) {
                oTable.fnClearTable();
            }
            oTable = $("#businessTable").dataTable({
                bRetrieve: true,
                "bLengthChange": false, //改变每页显示数据数量
                "iDisplayLength": 20,
                "bFilter": false, //过滤功能
                "bProcessing": false, // 是否显示取数据时的那个等待提示
                "bServerSide": true,//这个用来指明是通过服务端来取数据
                "sAjaxSource": "@Url.Content("~/API/ASMS/Query/SupplierPagedList")",//这个是请求的地址
                "fnServerData": retrieveData, // 获取数据的处理函数
                "aoColumns": [
                    {
                        //"sTitle": "Name",
                        "sWidth": '40px',
                        mData: null,
                        bSortable: false,
                        "mRender": function (data, type, full) {
                            return '<input type="checkbox" name="selectItem" data-id="' + full.id + '"/>';
                        }
                    },
                    {
                        "sWidth": '120px',
                        mData: "code",
                        bSortable: false
                    },
                    {
                        mData: "name",
                        bSortable: false
                    },
                    {
                        "sWidth": '140px',
                        mData: "contactName",
                        bSortable: false
                    },
                    {
                        "sWidth": '140px',
                        mData: "contactTelephone",
                        bSortable: false
                    },
                    {
                        bSortable: false,
                        mData: "status",
                        "sWidth": '120px',
                        mRender: function (data, type, full) {
                            if (data == 1) {
                                return "正常";
                            } else if (data == 2) {
                                return "已禁用";
                            } else if (data == 3) {
                                return "已删除";
                            } else if (data == 4) {
                                return "等待审核";
                            }
                            return 'unknown';
                        }
                    },
                    {
                        //"sTitle": "Operation",
                        mData: null,
                        "sWidth": '140px',
                        bSortable: false,
                        "mRender": function (data, type, full) {
                            if (full.status == 4) {
                                return '<a href="javascript:void(0)" onclick="onApprove(this);" data-id="' + full.id + '">通过</a><a href="javascript:void(0)" onclick="onReject(this);" data-id="' + full.id + '">拒绝</a>';
                            } else if (full.status == 3) {
                                return null;
                            }
                            return '<a href="javascript:void(0)" onclick="onDelete(this);" data-id="' + full.id + '">删除</a>';
                        }
                    }
                ]
                //,"aoColumnDefs": [{ "bSortable": false, "aTargets": [0, 1, 2, 3] }]
            });
        }

        // 3个参数的名字可以随便命名,但必须是3个参数,少一个都不行
        function retrieveData(sSource, aoData, fnCallback) {
            var start = 0;
            var limit = 0;
            var propertyNames = new Array();
            var sortIndex = 0;
            var sortDirection;
            for (var index = 0; index < aoData.length; index++) {
                if (aoData[index].name == 'iDisplayStart') {
                    start = aoData[index].value;
                } else if (aoData[index].name == 'iDisplayLength') {
                    limit = aoData[index].value;
                } else if (aoData[index].name.indexOf('mDataProp_') == 0) {
                    propertyNames.push(aoData[index].value);
                } else if (aoData[index].name.indexOf('iSortCol_') == 0) {
                    sortIndex = aoData[index].value;
                } else if (aoData[index].name.indexOf('sSortDir_') == 0) {
                    sortDirection = aoData[index].value;
                }
            }
            $.ajax({
                url: sSource,//这个就是请求地址对应sAjaxSource
                data: {
                    "filter": $('#com-search-input').val(),
                    "status": $('#query-status').val(),
                    'start': start,
                    'limit': limit
                },//, sortName: propertyNames[sortIndex], sortDirection: sortDirection },
                type: 'get',
                dataType: 'json',
                async: false,
                success: function (result) {
                    if (result.isSuccess) {
                        fnCallback({
                            sEcho: aoData[0].value,
                            iTotalRecords: result.data.total,
                            iTotalDisplayRecords: result.data.total,
                            aaData: result.data.items
                        });
                    }
                },
                error: function (msg) {
                }
            });
        }

        function onDelete(el) {
            var id = $(el).attr('data-id');  // save
            if (!id) {
                return;
            }
            if (window.confirm("是否要删除供应商?")) {
                $.ajax({
                    type: "POST",
                    url: "@Url.Content("~/api/asms/delete/Supplier")",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    data: JSON.stringify({ 'id': id }),
                    success: function (data) {
                        if (data.isSuccess) {
                            queryAdvertisementList();
                        } else {
                            alert(data.message);
                        }
                    },
                    error: function (data) {
                        alert('error');
                    }
                });
            }
        };
        function onDeleteMany(idArray) {
            if (idArray == null || idArray.length == 0)
                return;
            if (window.confirm("是否要删除选中的供应商?")) {
                $.ajax({
                    type: "POST",
                    url: "@Url.Content("~/api/asms/delete/SupplierMany")",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    data: JSON.stringify({ 'idArray': idArray }),
                    success: function (data) {
                        if (data.isSuccess) {
                            queryAdvertisementList();
                        } else {
                            alert(data.message);
                        }
                    },
                    error: function (data) {
                        alert('error');
                    }
                });
            }
        }

        function onApprove(el) {
            var id = $(el).attr('data-id');  // save
            if (!id) {
                return;
            }
            if (window.confirm("是否要审核通过供应商?")) {
                $.ajax({
                    type: "POST",
                    url: "@Url.Content("~/api/asms/approve/supplier")",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    data: JSON.stringify({ 'id': id }),
                    success: function (data) {
                        if (data.isSuccess) {
                            queryAdvertisementList();
                        } else {
                            alert(data.message);
                        }
                    },
                    error: function (data) {
                        alert('error');
                    }
                });
            }
        };

        function onReject(el) {
            var id = $(el).attr('data-id');  // save
            if (!id) {
                return;
            }
            if (window.confirm("是否要审核不通过该供应商?")) {
                $.ajax({
                    type: "POST",
                    url: "@Url.Content("~/api/asms/reject/supplier")",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    data: JSON.stringify({ 'id': id }),
                    success: function (data) {
                        if (data.isSuccess) {
                            queryAdvertisementList();
                        } else {
                            alert(data.message);
                        }
                    },
                    error: function (data) {
                        alert('error');
                    }
                });
            }
        };

        function onApproveMany(idArray) {
            if (idArray == null || idArray.length == 0)
                return;
            if (window.confirm("是否要审核通过选中的供应商?")) {
                $.ajax({
                    type: "POST",
                    url: "@Url.Content("~/api/asms/approve/supplierMany")",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    data: JSON.stringify({ 'idArray': idArray }),
                    success: function (data) {
                        if (data.isSuccess) {
                            queryAdvertisementList();
                        } else {
                            alert(data.message);
                        }
                    },
                    error: function (data) {
                        alert('error');
                    }
                });
            }
        };

        function onRejectMany(idArray) {
            if (idArray == null || idArray.length == 0)
                return;
            if (window.confirm("是否要审核不通过选中的供应商?")) {
                $.ajax({
                    type: "POST",
                    url: "@Url.Content("~/api/asms/reject/supplierMany")",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    data: JSON.stringify({ 'idArray': idArray }),
                    success: function (data) {
                        if (data.isSuccess) {
                            queryAdvertisementList();
                        } else {
                            alert(data.message);
                        }
                    },
                    error: function (data) {
                        alert('error');
                    }
                });
            }
        };
    </script>
}
